<template>
  <div class="map-operation-panel" :class="{ 'panel-visible': isVisible }">
    <div class="handlers-list">
      <button 
        v-for="handler in allHandlers" 
        :key="handler.type"
        class="handler-button"
        :class="{ 'handler-active': handler.active }"
        @click="toggleHandler(handler)"
        :title="handler.description || handler.name"
      >
        <span class="button-icon">{{ getHandlerIcon(handler.type) }}</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { mapController } from './MapController'

const props = defineProps<{
  mode: string
  isVisible: boolean
}>()

const emit = defineEmits<{
  close: []
}>()

const allHandlers = computed(() => {
  const handlers: any[] = []
  
  // 添加默认处理器
  const defaultHandlers = mapController.getDefaultHandlers()
  handlers.push(...defaultHandlers)
  
  // 添加当前模式的处理器
  if (props.mode) {
    const modeHandlers = mapController.getHandlersForMode(props.mode)
    handlers.push(...modeHandlers)
  }
  
  return handlers
})

const currentModeName = computed(() => {
  if (!props.mode) return ''
  const modeNames: Record<string, string> = {
    'Yukoume': '有口么',
    'FishingSpots': '钓点',
    'FishingCircle': '钓鱼圈',
    'Activities': '活动',
    'Profile': '个人中心'
  }
  return modeNames[props.mode] || props.mode
})

const getHandlerIcon = (type: string) => {
  const iconMap: Record<string, string> = {
    'pickup-point': '📍',
    'draw-line': '✏️',
    'measure': '📏',
    'mark': '📌',
    'area': '📐',
    'move-to-current': '📍',
    'map-type-switch': '🗺️',
    'polygon-draw': '⬟'
  }
  return iconMap[type] || '⚙️'
}

const toggleHandler = (handler: any) => {
  
  if (handler.active) {
    handler.deactivate()
  } else {
    handler.activate()
  }
}


watch(() => props.mode, (newMode, oldMode) => {
  console.log('MapOperationPanel: mode changed from', oldMode, 'to', newMode)
  if (newMode && newMode !== oldMode) {
    console.log('MapOperationPanel: 模式已切换，等待用户操作激活')
  }
})
</script>

<style scoped>
.map-operation-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 60px;
  height: 100%;
  background: var(--bg-overlay);
  border-left: 1px solid var(--border);
  backdrop-filter: blur(10px);
  z-index: 1000;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
}

.panel-visible {
  transform: translateX(0);
}



.handlers-list {
  flex: 1;
  padding: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  align-items: center;
}

.handler-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  margin: 4px auto;
  padding: 0;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s ease;
  box-sizing: border-box;
}

.handler-button:hover {
  background: var(--bg-card);
  border-color: var(--border-hover);
  transform: scale(1.05);
}

.handler-active {
  background: var(--accent-primary-bg);
  border-color: var(--accent-primary);
  color: var(--accent-primary);
  box-shadow: var(--shadow-active);
}

.button-icon {
  font-size: 22px;
  width: 24px;
  text-align: center;
  flex-shrink: 0;
  line-height: 1;
}

@media (max-width: 768px) {
  .map-operation-panel {
    position: fixed;
    top: 0;
    bottom: 0;
    left: auto;
    right: 0;
    width: 60px;
    height: 100%;
    transform: translateX(100%);
  }
  
  .panel-visible {
    transform: translateX(0);
  }
  
  .handlers-list {
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }
  
  .handler-button {
    width: 48px;
    height: 48px;
    margin: 0;
  }
}
</style>